;; The first four lines of this file were added by Dracula.
;; They tell DrScheme that this is a Dracula Modular ACL2 program.
;; Leave these lines unchanged so that DrScheme can properly load this file.
#reader(planet "reader.rkt" ("cce" "dracula.plt") "modular" "lang")
(require "IMask.lisp")
(require "iimage.lisp")
(require "icommon.lisp")

(module MMask
  (include-book "list-utilities" :dir :teachpacks)
  (include-book "io-utilities" :dir :teachpacks)
  (include-book "binary-io-utilities" :dir :teachpacks)
  (import IImage)
  (import ICommon)
  
  (defun maskPixelIsBlack (pixel maskpixel)
    (let* ((currR (car pixel))
           (currg (cadr pixel))
           (currB (caddr pixel))
           (maskR (car maskpixel))
           (maskG (cadr maskpixel))
           (maskB (caddr maskpixel)))
      (if (and (equal maskR 255) (equal maskG 255) (equal maskB 255))
          (createRGB 0 0 0)
          (pixel))))
  
  (defun maskPixel (currTree maskTree x y treeWidth treeHeight)
    (let* ((nextX (getNextXCoordinate x treeWidth))
           (nextY (getNextYCoordinate x y treeWidth))
           (currPixel (getPixel currTree x y))
           (maskPixel (getPixel maskTree x y))
           (maskIt (maskPixelIsBlack currPixel maskPixel))
           (newR (car maskIt))
           (newG (cadr maskIt))
           (newB (caddr maskIt)))
      (if (isValidPixel nextX nextY treeWidth treeHeight)
          (maskPixel (insertPixel currTree x y newR newG newB) nextX nextY treeWidth treeHeight)
          (insertPixel currTree x y newR newG newB))))
  
  (defun mask (currTree maskTree)
    (let* ((width  (getWidth currTree))
           (height (getHeight masktree)))
    (maskPixel currTree maskTree 1 height width height)))
  
  (defun MainMask (tree args)
    (let* ((maskTree (bmp->tree (binary-file->byte-list (car args) state))))
      (mv (mask tree maskTree) (list "Sucessfully masked the image"))))
              
  (export IMask))